package com.voicechat.firstmet.view.recyclerview.mo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.voicechat.firstmet.R;


/**
 * Created by Lym on 2018/1/6.
 */

public class MoCardLayoutManager extends RecyclerView.LayoutManager {
    private static final String TAG = "swipecard";
    private Context context;
    public static int MAX_SHOW_COUNT = 5;
    public static float SCALE_GAP = 0.10f;
    public static int TRANS_Y_GAP;
    public int mTopOffset = 0;
    public float scaleb=0.88f ;//底部的比例
    private double speedRatio=1.0;//滑动比(控制滑动速度)
    private OnVisiItem onVisiItem;

    public MoCardLayoutManager(Context context) {
        this.context=context;
        //平移时, 需要用到的参考值
        TRANS_Y_GAP = (int) (20 * context.getResources().getDisplayMetrics().density);
    }

    public void setOnVisiItem(OnVisiItem onVisiItem) {
        this.onVisiItem = onVisiItem;
    }

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        //必须要实现的方法
        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public MoCardLayoutManager setTopOffset(int topOffset) {
        mTopOffset = topOffset;
        return this;
    }
    //滑动速度的设置,横向滑动,通过speedRatio 比例来设置 速度
    @Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int x=super.scrollHorizontallyBy((int) (dx*speedRatio), recycler, state);
        return x;
    }
    //滑动速度的设置,纵向滑动,通过speedRatio 比例来设置 速度
    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int y=super.scrollVerticallyBy((int) (dy*speedRatio), recycler, state);
        return y;
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
//        Log.e(TAG, "onLayoutChildren() called with: recycler = [" + recycler + "], state = [" + state + "]");
        //在这个方法中进行View的布局操作.此方法会被调用多次.
        detachAndScrapAttachedViews(recycler);
        int itemCount = getItemCount();
        if (itemCount < 1) {
            return;
        }
        //top-3View的position
        int bottomPosition;
        //边界处理
        if (itemCount < MAX_SHOW_COUNT) {
            bottomPosition = 0;
        } else {
            bottomPosition = itemCount - MAX_SHOW_COUNT;
        }

        //从可见的最底层View开始layout，依次层叠上去
        for (int position = bottomPosition; position < itemCount; position++) {
            View view = recycler.getViewForPosition(position);
            addView(view);
            measureChildWithMargins(view, 0, 0);
            int widthSpace = getWidth() - getDecoratedMeasuredWidth(view);
            int heightSpace = getHeight() - getDecoratedMeasuredHeight(view);
            //我们在布局时，将childView居中处理，这里也可以改为只水平居中
            int offleft =  widthSpace / 2;//(int) context.getResources().getDimension(R.dimen.x60)
            int offup = (int) context.getResources().getDimension(R.dimen.y60);
            int offright = widthSpace / 2+getDecoratedMeasuredWidth(view);//getDecoratedMeasuredWidth(view)-(int) context.getResources().getDimension(R.dimen.x60)
            int offdown = mTopOffset == 0 ? (heightSpace / 2 + getDecoratedMeasuredHeight(view)) : (mTopOffset + getDecoratedMeasuredHeight(view));
            /*layoutDecoratedWithMargins(view,
                    0,
                    0,
                    getDecoratedMeasuredWidth(view),
                    (int) (offdown*scaleb));*/
            /*layoutDecoratedWithMargins(view, widthSpace / 2, mTopOffset == 0 ? (heightSpace / 2) : mTopOffset,
                    widthSpace / 2 + getDecoratedMeasuredWidth(view),
                    mTopOffset == 0 ? (heightSpace / 2 + getDecoratedMeasuredHeight(view)) :
                            (mTopOffset + getDecoratedMeasuredHeight(view)));*/
            layoutDecoratedWithMargins(view, widthSpace , mTopOffset == 0 ? (heightSpace ) : mTopOffset,
                    widthSpace  + getDecoratedMeasuredWidth(view),
                    mTopOffset == 0 ? (heightSpace + getDecoratedMeasuredHeight(view)) :
                            (mTopOffset + getDecoratedMeasuredHeight(view)));
            /**
             * TopView的Scale 为1，translationY 0
             * 每一级Scale相差0.05f，translationY相差7dp左右
             *
             * 拖动时，topView被拖动，Scale不变，一直为1.
             * top-1View 的Scale慢慢变化至1，translation也慢慢恢复0
             * top-2View的Scale慢慢变化至 top-1View的Scale，translation 也慢慢变化只top-1View的translation
             * top-3View的Scale要变化，translation岿然不动
             */

            //第几层,举例子，count =7， 最后一个TopView（6）是第0层，
            int level = itemCount - position - 1;
            //onVisiItem.getVisiItemPosition(level);
            //除了顶层不需要缩小和位移
            /*if (level > 0 *//*&& level < mShowCount - 1*//*) {
                //Log.i(TAG, "onLayoutChildren: -------"+level);
                //每一层都需要X方向的缩小
                //view.setScaleX(1 - SCALE_GAP * level);
                //Log.i(TAG, "onLayoutChildren: --level > 0---"+view.getScaleX() +"-----"+view.getScaleY());
                //前N层，依次向下位移和Y方向的缩小
                if (level < MAX_SHOW_COUNT - 1) {
                   view.setTranslationY(TRANS_Y_GAP * level);
                    //view.setScaleY(1 - SCALE_GAP * level);
                } else {//第N层在 向下位移和Y方向的缩小的成都与 N-1层保持一致
                   view.setTranslationY(TRANS_Y_GAP * (level - 1));
                   // view.setScaleY(1 - SCALE_GAP * (level - 1));
                }
            }else {
               // Log.i(TAG, "onLayoutChildren: ---level = 0--"+view.getScaleX() +"-----"+view.getScaleY());
            }*/
        }
    }



    public interface OnVisiItem{
        void getVisiItemPosition(int position);
    }
}
